from django.contrib.auth.models import AbstractUser
from django.db import models


# Create your models here.
# class UserInfo(AbstractUser):
#     pass

class CakeType(models.Model):
    id = models.AutoField(primary_key=True)
    typeName = models.CharField(max_length=32)

    def __str__(self):
        return self.typeName


# 蛋糕图片表
class CakeImg(models.Model):
    id = models.AutoField(primary_key=True)
    cake_id = models.ForeignKey(to='Cake', on_delete=models.PROTECT)
    img_file = models.ImageField(upload_to='img/', blank=True, null=True)  # 图片地址


# 蛋糕表
class Cake(models.Model):
    id = models.AutoField(primary_key=True)  # 编号
    name = models.CharField(max_length=32)  # 名字
    brief = models.CharField(max_length=255)  # 简介
    price = models.FloatField()  # 价格
    size = models.CharField(max_length=32)  # 大小
    sales_num = models.IntegerField()  # 销量
    status = models.CharField(max_length=32)  # 状态
    img = models.ForeignKey(to='CakeImg', blank=True, null=True, on_delete=models.PROTECT)  # 图片
    type = models.ForeignKey(to='CakeType', on_delete=models.PROTECT)  # 类型

    def __str__(self):
        return self.name


class Place(models.Model):
    id = models.AutoField(primary_key=True)
    uid = models.OneToOneField(to='UserInfo', on_delete=models.PROTECT)  # 用户id
    user_place = models.CharField(max_length=255)  # 地址
    user_postal = models.CharField(max_length=6, null=True)  # 邮编
    user_name = models.CharField(max_length=32)  # 收件人名字
    user_phone = models.CharField(max_length=11)  # 收件人手机号
    is_default = models.BooleanField()  # 是否为默认收货地址


class UserInfo(AbstractUser):
    """ 用户信息表 """
    nid = models.AutoField(primary_key=True)
    phone = models.CharField(max_length=11, null=True, unique=True)
    email = models.EmailField(null=True)
    is_vip = models.BooleanField(default=False)


# 轮播图表
class Rotation(models.Model):
    id = models.AutoField(primary_key=True)
    cake_id = models.ForeignKey(to='Cake', on_delete=models.PROTECT)
    img_file = models.ImageField(upload_to='img_rotation/', blank=True, null=True)  # 图片地址

    def __str__(self):
        return self.cake_id.name


# 热销图表
class HotSale(models.Model):
    id = models.AutoField(primary_key=True)
    cake_id = models.ForeignKey(to='Cake', on_delete=models.PROTECT)
    img_file = models.ImageField(upload_to='img_hot/', blank=True, null=True)  # 图片地址

    def __str__(self):
        return self.cake_id.name


# 首页展示表
class Show(models.Model):
    id = models.AutoField(primary_key=True)
    cake_id = models.ForeignKey(to='Cake', on_delete=models.PROTECT)

    def __str__(self):
        return self.cake_id.name
